document.write("<script language=javascript src='../../../../common/js/base.js'></script>");
document.write("<script language=javascript src='../../../../common/js/api/shun-api.js'></script>");
var app = new Vue({
 		el: '#main',
 		data: {
 			url : '', //请求的后端接口地址

 			params : { //请求后端接口的参数
 			},

 			username: 'xxx血糖值',

 			myStyle: { //图标的初始大小
 				width: '300px',
 				height: '200px',
 				padding: '5px',
 				float: 'left'
 			},
 			sugarValues: [],
 			iecharts : null
 		},

 		beforeCreate : function() {

 		},

 		created : function() {
 			console.log('created:' + this.$el);
 			// this.url = _ddoctor.apiUrl;
			this.url = spliceUrl('/sugar/shungan/chart');
 			this.params = _ddoctor.params;
 			this.username = _ddoctor.username || '';

 		},

 		beforeMount() {
 			let windowWidth = $(window).width();
 			let windowHeight = $(window).height();
 			let aspectRatio = (windowWidth / windowHeight).toFixed(2);

 			let iechartWidth = 0;
 			let styleHeight = 0;
 			if(aspectRatio > 1)
 			{
 				//宽高比大于1
 				//iechartHeight = Math.floor(100.2);
 				iechartHeight = Math.floor(windowHeight * 0.9);
 				//console.log('iechart=' + iechartHeight);
 				iechartWidth = parseInt(iechartHeight * aspectRatio);
 			}else {
 				 iechartWidth = parseInt(windowWidth * 0.95);
 				 iechartHeight = parseInt(iechartWidth * aspectRatio);
 			}

 			this.myStyle.width = (iechartWidth + "px");
 			this.myStyle.height = (iechartHeight + "px");
 		},

 		mounted() {
 			this.$nextTick(function(){

				this.iecharts = echarts.init(document.getElementById('iechart'), 'vintage');
				let options = {
						//baseOption : {
						    title: {
						        text: this.username + ' 血糖值'
						    },
						    tooltip: {
						        trigger: 'axis',
						       //  formatter: function (params) {
						       //      params = params[0];
						       //      var date = new Date(params.name);

						     		// let measurementTimeText = app.toMeasurementTimeText(params.value[2]);
						     		// if(measurementTimeText != '') {
						     		// 	measurementTimeText = '<br />(' + measurementTimeText + ')';
						     		// }


						       //      return params.name + measurementTimeText + ' : ' + params.value[1];
						       //  },
						    },
						    //legend: {
						    //    data: ['血糖值']
						   //},
						    //dataZoom: [{
							//	startValue: this.params.startTime
							//}, {
							//	type: 'inside'
							//}],
							toolbox: {
								left: 'center',
								feature: {
									dataZoom: {
										yAxisIndex: 'none'
									},
									dataView: {readOnly: true},
									magicType: {type: ['line', 'bar']},
									restore: {},
									saveAsImage: {}
								}
							},
							dataZoom: [{
								show: true,
								type: 'slider',
								start: 0,
								end: 100,
								handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
								handleSize: '80%',
								handleStyle: {
									color: '#fff',
									shadowBlur: 3,
									shadowColor: 'rgba(0, 0, 0, 0.6)',
									shadowOffsetX: 2,
									shadowOffsetY: 2
								}
							}],
						    grid: {
						    //    left: '3%',
						    //    right: '4%',
						    //    bottom: '3%',
						    //    containLabel: true
						    },
						    xAxis: {
						        //type: 'time',
						        //boundaryGap: false,
						        splitLine: {
	            					show: false
	        					},
	        					data: []
						    },
						    yAxis: {
						        type: 'value',
						        boundaryGap: [0, '100%'],
						        //splitLine: {
						        //    show: false
						        //}
						    },
							visualMap: {
								//type:"piecewise",
								top: 10,
								right: 5,
								precision:1,
								pieces: [{
									gt: 0,
									lte: 4.4,
									color: '#ffde33'
								}, {
									gt: 4.4,
									lte: 7.8,
									color: 'skyblue'
								}, {
									gt: 7.8,
									lte: 11,
									color: '#cc0033'
								}, {
									gt: 11,
									color: '#7e0023'
								}],
								outOfRange: {
									color: '#999'
								}
							},
						    series:
						        {
						            name: '血糖值',
							        type: 'line',
							        showSymbol: false,
							        hoverAnimation: false,
									//徐锋修改开始
									markPoint: {
										data: [
											{itemStyle:{normal:{color:'pink'}},type: 'max', name: '最大值'},
											{itemStyle:{normal:{color:'pink'}},type: 'min', name: '最小值'}
										]
									},
									markLine: {
										silent: true,
										data: [{
											yAxis: 4.4
										}, {
											yAxis: 7.8
										}, {
											yAxis: 11
										}]
									},
									//徐锋修改结束
						            data: []
						        }

						//},

						// media: [
						//             {
						//                 query: {
						//                     maxAspectRatio: 1
						//                 },
						//                 option: {
						//                     legend: {
						//                         right: 'center',
						//                         bottom: 0,
						//                         orient: 'horizontal'
						//                     },
						//                     series: [
						//                         {
						//                             radius: [20, '50%'],
						//                             center: ['50%', '30%']
						//                         },
						//                         {
						//                             radius: [30, '50%'],
						//                             center: ['50%', '70%']
						//                         }
						//                     ]
						//                 }
						//             },
						// ]
				};
				this.iecharts.setOption(options);
				this.getGlucometerData();
 			});
 		},

 		destroyed() {
 			console.log('destroyed');
 		},

	 	methods: {
	 		getGlucometerData : function() {
	 			app.iecharts.showLoading('default',  {
				  text: '正在努力加载...',
				  color: '#c23531',
				  textColor: '#000',
				  maskColor: 'rgba(254,248,239,1)',
				  zlevel: 0
				});
	 			//return;
	 			let uri = this.url;
	 			this.setStartTime($("#start-date").val());
	 			this.setEndTime($("#end-date").val());
	 			$.ajax({
	 				method: 'POST',
	 				url: this.url,
	 				contentType: 'application/json',
					dataType:"json",
	 				data: JSON.stringify(this.params),
	 				success: function(response) {
						if(!response.success) {
							app.iecharts.hideLoading();
							return;
						}
		 				app.iecharts.hideLoading();

		 				let datesValues = [];
		 				let sugarValues = [];
		 				if(response.result.recordList.length == 0) {
							errorBox('当前无记录，请添加');
							app.params.isFirst=0
		 				}else {
			 				for(let i = 0; i < response.result.recordList.length; i++) {
			 					let xAxisDate = response.result.recordList[i].recordTime.substr(5, 11);
			 					datesValues.push(xAxisDate);
			 					sugarValues.push(response.result.recordList[i].sugarValue);
			 				}
							app.params.startTime = response.result.startTime;
			 				app.params.endTime = response.result.endTime;
							app.params.isFirst=0;
			 				app.iecharts.setOption({
			 					xAxis:{data: datesValues},
			 					series: {
			 						data: sugarValues,
			 						smooth: true
			 					}
			 				});
		 				}
		 			},
		 			error: function(jqXHR, textStatus, errorThrown) {
		 				app.iecharts.hideLoading();
		 				alert(textStatus);
		 			}

	 			});
	 		},

	 		setStartTime : function(date) {
	 			this.params.startTime = date;
	 		},

	 		setEndTime : function(date) {
	 			this.params.endTime = date;
	 		},

	 		setUrl : function(url) {
	 			this.url = url;
	 		},

	 		setParams : function (params) {
	 			this.params = params;
	 		},

	 		toMeasurementTimeText : function(type) {
	 			switch(type) {
	 				case 0 :
	 					return '空腹';
	 				case 1 :
	 					return '早餐后';
	 				case 2 :
	 					return '午餐前';
	 				case 3 :
	 					return '午餐后';
	 				case 4 :
	 					return '晚餐前';
	 				case 5 :
	 					return '晚餐后';
	 				case 6 :
	 					return '睡前';
	 				case 7 :
	 					return '凌晨';
	 				case 8 :
	 					return '';
	 				default:
	 					return '';
	 			}

	 		},
	 	}
  });
